<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 23:04:50
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_topics extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_topics';
        $this->_pk            = 'topic_id';
        $this->_pre_cache_key = 'jamesonreadtopics_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    public function fetch_by_type($type)
    {
        if ($cache = $this->fetch_cache($type . '_type')) {
            return $cache;
        }
        if (is_array($type)) {
            $data = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('type', $type, 'in') . ' ORDER BY ordernum', array($this->_table));
        } else {
            $data = DB::fetch_all('SELECT * FROM %t WHERE type=%d ORDER BY ordernum', array($this->_table, $type));
        }
        if ($data) {
            $this->store_cache($type . '_type', $data);
        }
        return $data;
    }
    function fetch_pc_adv($type1,$type2=0){
        if ($cache = $this->fetch_cache('jamesonreadpcadv_'.$type1.'_'.$type2)) {
           return $cache;
        }
        $data = DB::fetch_all('SELECT * FROM %t WHERE type=%d '.($type2?' OR type=%d':'') .' ORDER BY ordernum', array($this->_table, $type1,$type2?$type2:''));
        if ($data) {
           $this->store_cache('jamesonreadpcadv_'.$type1.'_'.$type2,$data);
        }
        return $data;
    }
    function fetch_mb_adv($type1=11,$type2=12){
        if ($cache = $this->fetch_cache('jamesonreadpcadv_'.$type1.'_'.$type2)) {
           return $cache;
        }
        $data = DB::fetch_all('SELECT * FROM %t WHERE type=%d OR type=%d ORDER BY ordernum', array($this->_table, $type1,$type2));
        if ($data) {
           $this->store_cache('jamesonreadpcadv_'.$type1.'_'.$type2,$data);
        }
        return $data;
    }
    // 获取封装后带图书信息的专题
    public function fetch_books()
    {
        if ($cache = $this->fetch_cache('jamesonreadnewzhuanti')) {
           return $cache;
        }
        $data = $this->fetch_by_type(1);
        foreach ($data as $key => $value) {
            if (!$value['image']) {
                // 兼容老版
                $tmp = array();
                $value['book_1'] ? array_push($tmp, $value['book_1']) : '';
                $value['book_2'] ? array_push($tmp, $value['book_2']) : '';
                $value['book_3'] ? array_push($tmp, $value['book_3']) : '';
                $value['book_4'] ? array_push($tmp, $value['book_4']) : '';
                $value['book_5'] ? array_push($tmp, $value['book_5']) : '';
                $value['book_6'] ? array_push($tmp, $value['book_6']) : '';
                $value['image'] = $tmp;
            } else {
                $value['image'] = unserialize($value['image']);
            }
            $data[$key] = $value;
        }
        if ($data[0]['image']) {
            $data[0]['books'] = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('book_id', $data[0]['image'], 'in'), array('jamesonread_books'));
        }
        if ($data[1]['image']) {
            $data[1]['books'] = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('book_id', $data[1]['image'], 'in'), array('jamesonread_books'));
        }
        if ($data[2]['image']) {
            $data[2]['books'] = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('book_id', $data[2]['image'], 'in'), array('jamesonread_books'));
        }
        $this->store_cache('jamesonreadnewzhuanti',$data);
        return $data;
    }
}